home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_TOOL / TOOLS / TOOLS_WI / ICON_8 / BENCH_FO / DEAL.ICN < prev    next >
Text File  |  1990-04-06  |  3KB  |  121 lines

  1. ############################################################################
  2. #
  3. #    Name:    deal.icn
  4. #
  5. #    Title:    Deal bridge hands
  6. #
  7. #    Author:    Ralph E. Griswold
  8. #
  9. #    Date:    June 10, 1988
  10. #
  11. ############################################################################
  12. #  
  13. #     This program shuffles, deals, and displays hands in the game
  14. #  of bridge.  An example of the output of deal is
  15. #       ---------------------------------
  16. #  
  17. #                 S: KQ987
  18. #                 H: 52
  19. #                 D: T94
  20. #                 C: T82
  21. #  
  22. #       S: 3                S: JT4
  23. #       H: T7               H: J9863
  24. #       D: AKQ762           D: J85
  25. #       C: QJ94             C: K7
  26. #  
  27. #                 S: A652
  28. #                 H: AKQ4
  29. #                 D: 3
  30. #                 C: A653
  31. #  
  32. #       ---------------------------------
  33. #  
  34. #  Options: The following options are available:
  35. #  
  36. #       -h n Produce n hands. The default is 1.
  37. #  
  38. #       -s n Set the seed for random generation to n.  Different
  39. #            seeds give different hands.  The default seed is 0.
  40. #  
  41. ############################################################################
  42. #
  43. #  Links: options, post, shuffle
  44. #
  45. ############################################################################
  46.  
  47. link options, post, shuffle
  48. link post
  49.  
  50. global deck, deckimage, handsize, suitsize, denom, rank, blanker
  51.  
  52. procedure main(args)
  53.    local hands, opts
  54.  
  55.    Init__("deal")
  56.    deck := deckimage := string(&letters)    # initialize global variables
  57.    handsize := suitsize := *deck / 4
  58.    rank := "AKQJT98765432"
  59.    blanker := repl(" ",suitsize)
  60.    denom := &lcase[1+:suitsize]
  61.  
  62.    opts := options(args,"h+s+")
  63.    hands := \opts["h"] | 1
  64.    &random := \opts["s"]
  65.  
  66.    every 1 to hands do
  67.       display()
  68.  
  69.    Term__()
  70.  
  71. end
  72.  
  73. #  Display the hands
  74. #
  75. procedure display()
  76.    local layout, i
  77.    static bar, offset
  78.  
  79.    initial {
  80.       bar := "\n" || repl("-",33)
  81.       offset := repl(" ",10)
  82.       }
  83.  
  84.    deck := shuffle(deck)
  85.    layout := []
  86.    every push(layout,show(deck[(0 to 3) * handsize + 1 +: handsize]))
  87.  
  88.    write()
  89.    every write(offset,!layout[1])
  90.    write()
  91.    every i := 1 to 4 do
  92.       write(left(layout[4][i],20),layout[2][i])
  93.    write()
  94.    every write(offset,!layout[3])
  95.    write(bar)
  96. end
  97.  
  98. #  Put the hands in a form to display
  99. #
  100. procedure show(hand)
  101.    static clubmap, diamondmap, heartmap, spademap
  102.    initial {
  103.       clubmap := denom || repl(blanker,3)
  104.       diamondmap := blanker || denom || repl(blanker,2)
  105.       heartmap := repl(blanker,2) || denom || blanker
  106.       spademap := repl(blanker,3) || denom
  107.       }
  108.    return [
  109.       "S: " || arrange(hand,spademap),
  110.       "H: " || arrange(hand,heartmap),
  111.       "D: " || arrange(hand,diamondmap),
  112.       "C: " || arrange(hand,clubmap)
  113.       ]
  114. end
  115.  
  116. #  Arrange hands for presentation
  117. #
  118. procedure arrange(hand,suit)
  119.    return map(map(hand,deckimage,suit) -- ' ',denom,rank)
  120. end
  121.